package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class MyAddressPage extends Utils {
    public static String url = "http://49.235.61.184/?s=useraddress/index.html";

    public MyAddressPage() {
        super(url);
    }
    /**
     * 检查页面
     */
    public void checkMyAddressPage() throws IOException {
        //检查标题
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.am-flex.am-flex-items-center.am-margin-bottom-main > h1"));
        //检查“新增”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.control > button"));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());


    }
    /**
     * 测试一：新增地址失败- 省市区为空
     */
    public void addAddressFile1() throws InterruptedException, IOException {
        //点击“新增”按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.control > button")).click();
        //定位iframe元素
        WebElement webElement = driver.findElement(By.xpath("//iframe[@class=\"am-block\"]"));
        //切换到iframe子页面
        driver = driver.switchTo().frame(webElement);
        //清空输入框
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(2) > input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();

        //填写姓名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).sendKeys("霖霖");
        //填写手机号
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678901");
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("中国传媒大学");

        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    /**
     * 测试二：新增地址失败- 用户为空
     */
    public void addAddressFile2() throws InterruptedException, IOException {
        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);
        //填写手机号
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678901");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.\\30 .am-active > div > ul > li:nth-child(3) > span")).click();
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("中国传媒大学");
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 测试三：新增地址失败- 电话为空
     */
    public void addAddressFile3() throws InterruptedException, IOException {
        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);
        //填写姓名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).sendKeys("霖霖");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.\\30 .am-active > div > ul > li:nth-child(3) > span")).click();
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("中国传媒大学");
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 测试四：新增地址失败- 详情地址为空
     */
    public void addAddressFile4() throws InterruptedException, IOException {
        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);
        //填写姓名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).sendKeys("霖霖");
        //填写手机号
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678901");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.\\30 .am-active > div > ul > li:nth-child(3) > span")).click();
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(2000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 测试五：新增地址成功
     */
    public void addAddressSuc() throws InterruptedException, IOException {
        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);
        //填写姓名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).sendKeys("霖霖");
        //填写手机号
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678901");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.\\30 .am-active > div > ul > li:nth-child(3) > span")).click();
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("中国传媒大学");

        //设置为默认
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.am-form-success > div")).click();
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //切换回父页面的默认上下文
        driver.switchTo().defaultContent();
        String str = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.am-flex.am-flex-items-center.am-margin-bottom-main > h1")).getText();
        assert str.equals("我的地址");
    }

    /**
     * 测试六：编辑地址失败-用户名为空
     */
    public void updateAddressFile() throws InterruptedException, IOException {
        //点击“编辑”按钮
        driver.findElement(By.cssSelector("ul.address-list > li:nth-child(1) a.am-icon-edit.submit-popup")).click();
        //定位iframe元素
        WebElement webElement = driver.findElement(By.xpath("//iframe[@class=\"am-block\"]"));
        //切换到iframe子页面
        driver = driver.switchTo().frame(webElement);

        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);

        //修改电话号码
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678999");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu.am-active > div > ul > li:nth-child(23) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.am-active > div > ul > li:nth-child(1) > span")).click();
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("成都十七中");
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    /**
     * 测试七：编辑地址成功
     */
    public void updateAddressSuc() throws InterruptedException, IOException {
        //清空输入框
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).clear();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).clear();
        driver.findElement(By.cssSelector("#form-address")).clear();
        Thread.sleep(3000);

        //填写姓名
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div/form/div[1]/input")).sendKeys("小贺");
        //修改电话号码
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div:nth-child(3) > input")).sendKeys("12345678999");
        //选择省市区
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-suffix > div")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.province.am-cascader-menu.am-active > div > ul > li:nth-child(23) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.city.am-cascader-menu.am-active > div > ul > li:nth-child(1) > span")).click();
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-group.region-linkage > div > div.am-cascader.max-width > div.am-cascader-dropdown.am-active > div.am-cascader-panel > div.am-scrollbar.county.am-cascader-menu.am-active > div > ul > li:nth-child(1) > span")).click();
        //填写详细地址
        driver.findElement(By.cssSelector("#form-address")).sendKeys("成都十七中");
        //点击”保存“按钮
        driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div > form > div.am-form-popup-submit > button.am-btn.am-btn-primary.am-radius.am-btn-xs.btn-loading-example")).click();
        //屏幕截图
        Thread.sleep(1000);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());

        //切换回父页面的默认上下文
        driver.switchTo().defaultContent();
        String str = driver.findElement(By.cssSelector("body > div.body-content-container > div.body-content-formal-container > div.am-container.user-main > div.user-content > div > div.am-flex.am-flex-items-center.am-margin-bottom-main > h1")).getText();
        assert str.equals("我的地址");
    }
    /**
     * 测试八：删除地址成功
     */
    public void delAddressSuc() throws InterruptedException, IOException {
        //点击“删除”按钮
        driver.findElement(By.cssSelector("ul.address-list > li:nth-child(1)  a.iconfont.icon-delete.address-submit-delete")).click();
        //弹出提示窗，点击“确认”按钮
        driver.findElement(By.cssSelector("span.am-modal-btn[data-am-modal-confirm]")).click();
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
}
